import csv
import json
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


def parse_count(count):
    def removesuffix(s, suffix):
        if s.endswith(suffix):
            return s[:-len(suffix)]
        return s

    count_str = removesuffix(count, '+')
    if count_str.isdigit():
        return float(count_str)
    else:
        return float(removesuffix(count_str, '万')) * 10000


def get_reviews(driver, product_url):
    driver.get(product_url)
    time.sleep(5)

    # 点击"商品评价"
    comment_tab = driver.find_element(By.XPATH, '//li[@data-anchor="#comment"]')
    ActionChains(driver).move_to_element(comment_tab).click().perform()
    time.sleep(3)

    # 点击"只看当前商品评价"
    current_sku_checkbox = driver.find_element(By.XPATH, '//li[@class="comm-curr-sku"]//label')
    ActionChains(driver).move_to_element(current_sku_checkbox).click().perform()
    time.sleep(3)

    # 获取好评数
    positive_reviews = driver.find_element(By.XPATH,
                                           '//li[@data-tab="trigger" and @data-num="0" and contains(.,"好评")]')
    positive_reviews_count = parse_count(positive_reviews.find_element(By.TAG_NAME, 'em').text.strip('()'))

    # 获取中评数
    neutral_reviews = driver.find_element(By.XPATH,
                                          '//li[@data-tab="trigger" and @data-num="0" and contains(.,"中评")]')
    neutral_reviews_count = parse_count(neutral_reviews.find_element(By.TAG_NAME, 'em').text.strip('()'))

    # 获取差评数
    negative_reviews = driver.find_element(By.XPATH,
                                           '//li[@data-tab="trigger" and @data-num="0" and contains(.,"差评")]')
    negative_reviews_count = parse_count(negative_reviews.find_element(By.TAG_NAME, 'em').text.strip('()'))

    return positive_reviews_count, neutral_reviews_count, negative_reviews_count


print("Begin testing...")
driver = webdriver.Chrome()
url = "https://www.jd.com"
driver.maximize_window()
driver.implicitly_wait(2)

# 访问网站，清空旧cookies信息
driver.get(url)
time.sleep(5)
driver.delete_all_cookies()

# 加载 cookies信息
with open("cookies.txt", "r") as f:
    cookies = json.load(f)
for cookie in cookies:
    driver.add_cookie(cookie)

# 验证是否登录成功
driver.get(url)
time.sleep(3)

# 读取商品ID列表
sku_list = []
with open('../data_csv/product_id/data_sku_list.csv', 'r') as f:
    reader = csv.reader(f)
    next(reader)  # 跳过第一行变量名
    for row in reader:
        sku_list.append(row[0])

# 爬取每个商品的评价信息并保存到CSV文件
with open('../data_csv/comment/reviews.csv', 'w', newline='', encoding='utf-8') as csvfile:
    fieldnames = ['Product ID', 'Positive Reviews', 'Neutral Reviews', 'Negative Reviews']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()

    for sku in sku_list:
        product_url = f"https://item.jd.com/{sku}.html"
        positive_reviews, neutral_reviews, negative_reviews = get_reviews(driver, product_url)
        writer.writerow({
            'Product ID': sku,
            'Positive Reviews': positive_reviews,
            'Neutral Reviews': neutral_reviews,
            'Negative Reviews': negative_reviews
        })
        print(f"Data for product {sku} saved.")

print("All data saved to reviews.csv")

# 关闭浏览器
driver.quit()
